import serial
import serial.tools.list_ports



def get_ports():
    available_ports = []
    port_list = list(serial.tools.list_ports.comports())
    print(port_list)

    for port in port_list:
        available_ports.append(port.device)
        print(port.device)
    return available_ports

def open_port(port_name):
    ser = serial.Serial(port=port_name,
                        baudrate=921600,
                        bytesize=8,
                        stopbits=1,
                        parity='N')
    print(ser.is_open)
    if ser.is_open:
        print(port_name + '串口成功连接！')
    else:
        print(port_name + '串口连接失败！')

    return ser


if __name__ == '__main__':
    get_ports()
    open_port('COM1')